<?php
header("Content-Type: text/html; charset=iso-8859-1");
require_once 'include.php';
class match{
	var $id;
	var $fields;
	function match($id,$field = 'no',$saison = ""){
		if($saison){
			$table = (table_exists("matchs_$saison")) ? "matchs_$saison" : "matchs";
		}
		else $table = "matchs";
		$this->table = $table;
		$fields = describe("matchs");
		if($id == 'next' || $id == 'last'){
			$equipe = (func_num_args() > 2) ? func_get_arg(2) : "";
			$q = "select * from $table where equipe = '$equipe'";
			if($id == 'next'){
				$q .= " and date >= curdate() and (resultat not like '%-%' or resultat is null) order by date asc limit 1";
			}
			elseif($id == 'last'){
				$q .= " and date <= curdate() and resultat != '' and resultat != 'NULL' order by date desc limit 1";
			}
		}
		else $q = "select * from $table where no = '$id'";
		$r = ExecRequete($q);
		if($s = LigneSuivante($r)){
			foreach($fields as $field => $type){
				$this->$field = $s->$field;
			}
			$this->stat = $this->stats(0);
			$this->adversaire = (strstr(strtolower($this->local),"lutry") !== false) ? $this->visiteur : $this->local;
		}
		else{
			foreach($fields as $field_id => $type){
				$this->$field_id = ($field_id == $field) ? $id : "";
			}
			$this->stat = "";
			$this->adversaire = "";
		}
	}

	function format_resultat(){
		$tab = explode("-",$this->resultat);
		if(count($tab) == 2){
			$class = ((strstr($this->local,"LUTRY") !== FALSE && $tab[0] > $tab[1]) || (strstr($this->visiteur,"LUTRY") !== FALSE && $tab[0] < $tab[1])) ? "gagne" : "perdu";
			return "<span class = '$class'>$this->resultat</span>";
		}
		else return "";
	}

	function get_salle(){
		if($this->salle){
			$salle = new salle($this->salle);
			return $salle;
		}
		else return "";
	}

	function stats($detail = 0) {
      	$victoire = 0;
      	$defaite = 0;
		$tables = array();
		$q = "show tables";
		$r = ExecRequete($q);
		while($s = LigneSuivante($r)){
			$table = $s->{"Tables_in_".BASE};
			if(strpos($s->{"Tables_in_".BASE},"atchs") ==1){
				$tables[] = $s->{"Tables_in_".BASE};
			}
		}
		if($detail){
			$echo = "<h2>($this->equipe) ".(strtoupper($this->local))." - ".(strtoupper($this->visiteur))." : ".($this->stats(0))."</h2>\n";
			$print_table = new table();
			$fields = array("date","local","visiteur","resultat");
			foreach($fields as $field){
				$$field = new table_field($field);
				if($field == 'date' || $field == 'resultat') $$field->styleclass = 'center';
				$print_table->fields[] = $$field;
			}
			$print_table->order = date;
		}
		$query_visiteur = (strpos($this->visiteur,"LUTRY") === FALSE) ? preg_replace("/ [IV]+$/","",$this->visiteur) : ereg_replace(" [IV]+$","",$this->local);
		$query_visiteur = str_ireplace("VOLLEY ","",$query_visiteur);
		$tab_visiteur = explode(" ",$query_visiteur);
		if(count($tab_visiteur) > 1){
			$nbcar = 0;
			foreach($tab_visiteur as $word_visiteur){
				if(strlen($word_visiteur) > $nbcar){
					$nbcar = strlen($word_visiteur);
					$query_visiteur = $word_visiteur;
				}
			}
		}
		foreach($tables as $table){
			if(strlen($this->equipe) > 2){
				$query_equipe = ereg_replace("[AB]$","",$this->equipe);
				if(strpos($query_equipe,"2") != 1) $query_equipe = ereg_replace("2$","",$query_equipe);
				$q = "select local,visiteur,resultat, date from $table where equipe like '$query_equipe%' and ((local like '%LUTRY%' and visiteur like '%$query_visiteur%') or (local like '%$query_visiteur%' and visiteur like '%LUTRY%')) and resultat like '%-%' order by date desc";
			}
			else $q = "select local,visiteur,resultat, date from $table where equipe like '$this->equipe%' and ((local like '%LUTRY%' and visiteur like '%$query_visiteur%') or (local like '%$query_visiteur%' and visiteur like '%LUTRY%')) and resultat like '%-%' order by date desc";
			vbcll_log($q);

			$r = ExecRequete($q);
			while($s = LigneSuivante($r)){
				$resultat = explode('-',$s->resultat);
				if(count($resultat)>1){
					if(($resultat[0] < $resultat[1] && strstr($s->visiteur,'LUTRY') !== FALSE) ||
						($resultat[0] > $resultat[1] && strstr($s->local,'LUTRY') !== FALSE) ){
							 $victoire++;
							if($detail){
								$row = new table_row();
								foreach($fields as $field){
									if($field == 'resultat') $print = "<span class = 'oui'>$s->resultat</span>";
									elseif($field == 'date') $print = $s->date;
									else $print = $s->$field;
									$row->cols[$field] = $print;
								}
								$print_table->rows[] = $row;
							}
					}
					elseif($resultat[0] != $resultat[1]) {
						$defaite++;
						if($detail){
							$row = new table_row();
							foreach($fields as $field){
								if($field == 'resultat') $print = "<span class = 'non'>$s->resultat</span>";
								elseif($field == 'date') $print = $s->date;
								else $print = $s->$field;
								$row->cols[$field] = $print;
							}
							$print_table->rows[] = $row;
						}
					}
				}
			}
		}
		if($detail){
			$echo .= $print_table->print_header();
			$echo .= $print_table->print_body();
			$echo .= $print_table->print_footer();
			return $echo;
		}
		else{
			$stat = (strstr($this->local,'LUTRY') !== FALSE) ? $victoire."-".$defaite : $defaite."-".$victoire;
			return $stat;
		}
  	}

	function update(){
		$no_jours = array("di","lu","ma","me","je","ve","sa");
		$fields = describe('matchs');
		if($this->no){
			$q = "replace into matchs set ";
			$tab_date = explode("-",$this->date);
			$this->jour = strtoupper($no_jours[date("w",mktime(0, 0, 0, $tab_date[1]  , $tab_date[2], $tab_date[0]))]);
			foreach($fields as $field => $type){
				$q .= $field." = '".addslashes($this->$field)."', ";
			}
			$q = substr($q,0,-2);
			$this->stat = $this->stats(0);
			$this->adversaire = (strstr(strtolower($this->local),"lutry") !== false) ? $this->visiteur : $this->local;
			if(strstr(strtolower($this->local),"lutry") !== false || $this->marquage){
				$q1 = "select * from matchs where no = '$this->no'";
				$r1 = ExecRequete($q1);
				if(!LigneSuivante($r1)){
					$q1 = "select nom,prenom,email from membres inner join membre_commissions on membres.id = membre_id where fonction like '%marquage%'";
					$r1 = ExecRequete($q1);
					$email_vbcll = ($s1 = LigneSuivante($r1)) ? $s->email : "vbcll@vbcll.ch";
					$from = "vbcll@vbcll.ch";
					$titre = "[VBCLL] Nouveau match à marquer";
					$subject = encode_iso88591($titre);
					$headers =    "MIME-Version: 1.0\r\n" .
					               "Content-type: text/plain; charset=utf-8; format=flowed\r\n" .
					               "Content-Transfer-Encoding: 8bit\r\n" .
					               "From: $from\r\n" .
					               "X-Mailer: PHP" . phpversion();
					$msg = "Salut,\n\n";
					$msg .= "Un nouveau match a été inscrit sur le site web pour lequel il faut un marqueur : \n\n";
					$msg .= $this->jour." ".$tab_date[2].".".$tab_date[1].".".$tab_date[0]." à ".$this->heure."\n";
					$salle = $this->get_salle();
					$msg .= $salle->nom." ".$salle->localite;
					mail($to, $subject, $msg, $headers);
				}
			}
			if($r = ExecRequete($q)) return mysql_affected_rows();
			else return false;
		}
		else return false;
	}

	function form_edit($table_row_id = "",$all = ""){
		global $jours;
		$fields = describe('matchs');
		if($this->no) echo "<h2>Modifier le match $this->no</h2>";
		else echo "<h2>Nouveau match</h2>";
		echo "<form action = 'update_match_admin.php' method = 'post' id = 'form_match' name = 'form_match' />\n";
		foreach($fields as $field => $type){
			$display = (strpos($field,"marqueur") !== FALSE && !$this->marquage) ? "style = 'display: none; visibility: hidden'" : "";
			echo leftCol("<span id = 'row_$field' $display>".$field." : </span>",30);
			if($field == 'equipe'){
				$echo = "<select name = 'equipe'>\n";
				$echo .= "<option value = ''>choisir...</option>\n";
				$q = "select id, nom_complet,nom from equipes order by num";
				$r = ExecRequete($q);
				while($s = LigneSuivante($r)){
					$selected = ($s->nom == $this->equipe) ? "selected = 'selected'" : "";
					$echo .= "<option value = '$s->nom' $selected>$s->nom_complet</option>\n";
				}
				$echo .= "</select>";
				echo rightCol($echo,69);
			}
			elseif($field == 'jour'){
				$echo = "<input type = 'hidden' name = 'jour' value = '$this->jour' /><span id = 'print_jour'>".$jours[strtolower($this->jour)]."</span>";
				echo rightCol($echo,69);
			}
			elseif($field == 'resultat'){
				$resultats = (strpos($this->equipe,'M17') !== FALSE || strpos($this->equipe,'M15') !== FALSE) ? array("","2-0","2-1","1-2","0-2","1-1") : array("","3-0","3-1","3-2","2-3","1-3","0-3");
				if(strpos($this->equipe,'M19G') !== FALSE) $resultats = array("","3-0","2-1","1-2","0-3");
				$echo = "<select name = 'resultat'>\n";
				foreach($resultats as $resultat){
					$selected = ($resultat == $this->resultat) ? "selected = 'selected'" : "";
					$echo .= "<option value = '$resultat' $selected>$resultat</option>\n";
				}
				$echo .= "</select>\n";
				echo rightCol($echo,69);

			}
			elseif($field == 'salle'){
				$salles_lutry = array(610,611,620,600,601);
				// $echo = "<div style = 'position: relative'>";
				$salle = new salle($this->salle);
				$value = "[$salle->no] $salle->nom";
				$onblur = "onblur = \"if(";
				foreach($salles_lutry as $no_lutry) $onblur .= "document.getElementById('salle_id').value == $no_lutry || ";
				$onblur = substr($onblur,0,-4)."){
					document.getElementById('marquage').checked = true;
					document.getElementById('row_marqueur1').style.display = 'inline';
					document.getElementById('row_marqueur1').style.visibility = 'visible';
					document.getElementById('row_marqueur2').style.display = 'inline';
					document.getElementById('row_marqueur2').style.visibility = 'visible';
					document.getElementById('print_marqueur1').style.display = 'inline';
					document.getElementById('print_marqueur1').style.visibility = 'visible';
					document.getElementById('print_marqueur2').style.display = 'inline';
					document.getElementById('print_marqueur2').style.visibility = 'visible';
					}
					else{
						document.getElementById('marquage').checked = false;
						document.getElementById('row_marqueur1').style.display = 'none';
						document.getElementById('row_marqueur1').style.visibility = 'hidden';
						document.getElementById('row_marqueur2').style.display = 'none';
						document.getElementById('row_marqueur2').style.visibility = 'hidden';
						document.getElementById('print_marqueur1').style.display = 'none';
						document.getElementById('print_marqueur1').style.visibility = 'hidden';
						document.getElementById('print_marqueur2').style.display = 'none';
						document.getElementById('print_marqueur2').style.visibility = 'hidden';

					}\"";
				$echo = "<input type = 'text' size = '35' name = 'salle' id = 'salle' value = '$value' $onblur/>";
				$echo .= "<span id='indicator1_salle' style='display: none'><img src='images/spinner.gif' alt='recherche...' /></span>";
				$autocomplete = "<div id='autocomplete_choices_salles' class='autocomplete'></div>";
				$echo .= "<input type = 'hidden' name = 'salle_id' id = 'salle_id' value = '$this->salle' />";
				// $echo .= "</div>";
				echo rightCol($echo,69);
			}
			elseif($field == 'date'){
				$date_array = explode("-",$this->date);
				$jour = (count($date_array) == 3) ? $date_array[2] : "";
				$mois = (count($date_array) == 3) ? $date_array[1] : "";
				$annee = (count($date_array) == 3) ? $date_array[0] : "";
				$echo = "<input type = 'text' name = 'djour' id = 'djour' value = '$jour' size = '2' maxlength = '2' onkeyup = \"if(this.value.length == 2) document.form_match.mois.focus()\" onblur  \"writediv(print_jour_from_date(document.getElementById('annee').value,document.getElementById('mois').value,document.getElementById('djour').value),'print_jour')\" />.";
				$echo .= "<input type = 'text' name = 'mois' id = 'mois' value = '$mois' size = '2' maxlength = '2' onkeyup = \"if(this.value.length == 2) document.form_match.annee.focus()\" onblur = \"writediv(print_jour_from_date(document.getElementById('annee').value,document.getElementById('mois').value,document.getElementById('djour').value),'print_jour')\"/>.";
				$echo .= "<input type = 'text' name = 'annee' id = 'annee' value = '$annee' size = '4' maxlength = '4' onblur = \"writediv(print_jour_from_date(document.getElementById('annee').value,document.getElementById('mois').value,document.getElementById('djour').value),'print_jour')\"/>";				$echo  .= "<input type = 'hidden' name = 'date' value = '$this->date' />";
				echo rightCol($echo,69);
			}
			elseif($field == 'heure'){
				$heure_array = explode(":",$this->heure);
				$heure = (count($heure_array) == 3) ? $heure_array[0] : "";
				$minute = (count($heure_array) == 3) ? $heure_array[1] : "";
				$seconde = (count($heure_array) == 3) ? $heure_array[2] : "";
				$echo = "<input type = 'text' name = 'heures' value = '$heure' size = '2' maxlength = '2' onkeyup = \"if(this.value.length == 2) document.form_match.minute.focus()\" />:";
				$echo .= "<input type = 'text' name = 'minute' value = '$minute' size = '2' maxlength = '2' />";
				$echo .= "<input type = 'hidden' name = 'seconde' value = '$seconde' />";
				$echo  .= "<input type = 'hidden' name = 'heure' value = '$this->heure' />";
				echo rightCol($echo,69);

			}
			elseif($field == 'marquage'){
				$checked = ($this->$field == 1) ? "checked = 'checked'" : "";
				$display_marqueurs = "onchange = \"if(this.checked){
				document.getElementById('row_marqueur1').style.display = 'inline';
				document.getElementById('row_marqueur1').style.visibility = 'visible';
				document.getElementById('row_marqueur2').style.display = 'inline';
				document.getElementById('row_marqueur2').style.visibility = 'visible';
				document.getElementById('print_marqueur1').style.display = 'inline';
				document.getElementById('print_marqueur1').style.visibility = 'visible';
				document.getElementById('print_marqueur2').style.display = 'inline';
				document.getElementById('print_marqueur2').style.visibility = 'visible';
				}
				else{
					document.getElementById('row_marqueur1').style.display = 'none';
					document.getElementById('row_marqueur1').style.visibility = 'hidden';
					document.getElementById('row_marqueur2').style.display = 'none';
					document.getElementById('row_marqueur2').style.visibility = 'hidden';
					document.getElementById('print_marqueur1').style.display = 'none';
					document.getElementById('print_marqueur1').style.visibility = 'hidden';
					document.getElementById('print_marqueur2').style.display = 'none';
					document.getElementById('print_marqueur2').style.visibility = 'hidden';

				}\"";
				echo rightCol("<input type = 'checkbox' id = '$field' name = '$field' value = '1' $checked $display_marqueurs />",69);
			}

			elseif($field == 'marqueur1'){
				$display = (!$this->marquage) ? " display: none; visibility: hidden;" : "";
				$echo = "<div style = 'position: relative;$display' id = 'print_marqueur1'>";
				$marqueur = new membre($this->marqueur1);
				$value = ($this->marqueur1) ? "$marqueur->prenom $marqueur->nom" : "";
				$echo .= "<input type = 'text' size = '20' name = 'marqueur1' id = 'marqueur1' value = '$value'/>";
				$echo .= "<span id='indicator1_marqueur1' style='display: none'><img src='images/spinner.gif' alt='recherche...' /></span>";
				$autocomplete .= "<div id='autocomplete_choices_marqueur1' class='autocomplete'></div>";
				$echo .= "<input type = 'hidden' name = 'marqueur1_id' id = 'marqueur1_id' value = '$this->marqueur1' />";
				$echo .= "</div>";
				echo rightCol($echo,69);
			}
			elseif($field == 'marqueur2'){
				$display = (!$this->marquage) ? " display: none; visibility: hidden;" : "";
				$echo = "<div style = 'position: relative;$display' id = 'print_marqueur2'>";
				$marqueur = new membre($this->marqueur2);
				$value = ($this->marqueur2) ? "$marqueur->prenom $marqueur->nom" : "";
				$echo .= "<input type = 'text' size = '20' name = 'marqueur2' id = 'marqueur2' value = '$value'/>";
				$echo .= "<span id='indicator1_marqueur2' style='display: none'><img src='images/spinner.gif' alt='recherche...' /></span>";
				$autocomplete .= "<div id='autocomplete_choices_marqueur2' class='autocomplete'></div>";
				$echo .= "<input type = 'hidden' name = 'marqueur2_id' id = 'marqueur2_id' value = '$this->marqueur2' />";
				$echo .= "</div>";
				echo rightCol($echo,69);
			}
			else echo rightCol("<input type = 'text' name = '$field' value = '".$this->$field."'  onblur = 'this.value = this.value.toUpperCase()' />",69);
		}
		echo "<br class = 'clear'/>";
		$text = ($this->no) ? "modifier" : "nouveau";
		echo "<p class = 'center'><input type = 'hidden' name = 'table_row_id' value = '$table_row_id' /><input type = 'hidden' name = 'all' value = '1' /><button type = 'button' name =  'action' value = 'edit' onclick = 'update_match()'>$text</button><button onclick = 'writediv(\"\",\"center_box\");center_box()' type = 'button' >annuler</button></p></form>\n";
		echo $autocomplete;
	}

	function delete(){
		if($this->no){
			$q = "delete from $this->table where no = '$this->no' limit 1";
			return (ExecRequete($q)) ? 1 : 0;
		}
		else return 0;
	}

	function presences(){
		$annee = (date('m')>7) ? date('Y') : date('Y')-1;
		$q = "select membres.id as id, prenom , comptabilite.licence as licence, no_match
			from membres
			inner join membre_equipes on (membre_equipes.membre_id = membres.id)
			inner join equipes on (membre_equipes.equipe_id = equipes.id)
			inner join matchs on (matchs.equipe = equipes.nom)
			left join membre_matchs on (membre_matchs.membre_id = membres.id and no_match = '$this->no')
			left join comptabilite on (membres.id = comptabilite.membre_id)
			where matchs.no = '$this->no'
			order by licence, prenom";
		$r = ExecRequete($q);
		$return = array();
		while($s = LigneSuivante($r)){
			if($s->licence && $s->no_match) $return[$s->prenom.":".$s->id] = "present";
			elseif($s->licence) $return[$s->prenom.":".$s->id] = "absent";
			else $return[$s->prenom.":".$s->id] = "licence";
		}
		return $return;
	}
}
?>
